<?php
namespace app\admin\controller;
use app\common\controller\Adminbase;
use \think\Loader;
use \think\Db;

// +----------------------------------------------------------------------
// | XpCms
// +----------------------------------------------------------------------
// | Copyright (c) 2021-2099
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Mercurio（Xp） <725668993@qq.com>
// +----------------------------------------------------------------------

/*
** 文章 控制器
*/

class Article extends Adminbase {
	private static $_category = null; 	// 数据表对象
	private static $_article = null; 	// 数据表对象
	
	// 优先加载
	public function _initialize() {
		parent::_initialize();
		
		// 实例化数据表模型
		self::$_category = Loader::model('Category');
		self::$_article = Loader::model('Article');
		
	}
	
	// 文章列表
	public function index() {
		
        // 处理查询条件
        $where = array();
		// 当前页
		$nowPage = 1;
		if(isset($_GET['page']) && $_GET['page']){
			$nowPage = input('param.page/d');
			$nowPage = $nowPage > 0 ? $nowPage : 1;
		}
		$cid = input('param.cid/d') ? input('param.cid/d') : '';
		if(isset($cid)){
		    $mcode = Db::name('category')->where('id', $cid)->value('mcode');
		    $tablename = Db::name('model')->where('mcode', $mcode)->value('tablename');
		    $this -> assign('cid', $cid);
		    $where['cid'] = ['=', $cid];
		}
		
		if(input('param.title')){
		    $title = input('param.title');
		    $this -> assign('title', $title);
		}
		
		
		
		
		// 每页显示条数，默认 10 条
		$pagesize=input('param.pagesize')?input('param.pagesize'):10;
		
		
		// 时间
		$whereTimeS = array();
		$whereTimeE = array();
		if(isset($_GET['startime']) && $_GET['startime'] && isset($_GET['endtime']) && $_GET['endtime']){
			// 检查时间日期的合法性
			$startime = input('param.startime');
			$endtime = input('param.endtime');
			$starTimes = date('Y-m-d', strtotime($startime));
			$endTimes = date('Y-m-d', strtotime($endtime));
			if($startime === $starTimes){ }else{
				echo '<script>alert("起始时间不合法！");</script>';
				$startime = '';
			}
			if($endtime === $endTimes){ }else{
				echo '<script>alert("结束时间不合法！");</script>';
				$endtime = '';
			}
			$FindStarTime = strtotime($startime);
			$FindendTime = strtotime($endtime) + 86400 - 1;
			
			if($FindStarTime > $FindendTime){
				echo '<script>alert("开始时间不能大于结束时间！");</script>';
				$startime = '';
				$endtime = '';
			}
			// 区间查询
			if($startime && $endtime){
				$where['time_report'] = array(
									array('egt', $FindStarTime),
									array('elt', $FindendTime),
									'and'
								);
			}
		}else{
			$startime = '';
			$endtime = '';
		}
		
		// 标题
		if(isset($_GET['title']) && $_GET['title']){
			$title = input('param.title');
			$where['title'] = array('like', '%' . $title . '%');
		}
		
		// 获取分页后的文章列表
		$pageData = self::$_article -> articleLists($where, $nowPage, $pagesize,'id desc', $tablename);
		
// 		// 替换
// 		foreach($pageData['lists'] as $key => &$val){
// 			// 处理缩略图显示
// // 			$val['litpic'] = show_image($val['litpic']);
// 			// 获取分类名称
// 			$val['cname'] = self::$_category -> getName($val['cid']);
// 		}
		
		$showDatas = array(
				'lists' 		=> $pageData['lists'],
				'allpages' 		=> $pageData['allpages'], 	// 总页数
				'nowpage' 		=> $pageData['nowpage'], 	// 当前页
				'page' 		    => $pageData['page'], 	// 当前页
				'startime' 		=> $startime,
				'endtime' 		=> $endtime,
				'title' 		=> $title,
				'pagesize'      => pagesize($pagesize)
			);
		
		$this -> assign($showDatas);
		return view('index');
	}
	
	// 新增文章页面
	public function add() {
		$cid = input('cid') ? input('cid') : 0;
		if($cid !== 0){
		    // 输出对应模型的自定义字段
		    $mcode = get_mcode($cid);
		    $fields = Db::name('field')
                ->where('moduleid','=',$mcode)
                ->order('sort ASC,id ASC')
                ->select();
            foreach ($fields as $k=>$v){
                if($fields[$k]['setup']){
                    $fields[$k]['setup'] = string2array($v['setup']);
                    if(array_key_exists('options',$fields[$k]['setup'])){
                        $fields[$k]['setup']['options'] = explode("\n",$fields[$k]['setup']['options']);
                        foreach ($fields[$k]['setup']['options'] as $kk=>$vv){
                            $fields[$k]['setup']['options'][$kk] = trim_array_element(explode("|",$fields[$k]['setup']['options'][$kk]));

                        }
                    }
                }

            }
            // dump($fields);
            $this -> assign('fields',$fields);
            $this -> assign('cid',$cid);
            $this -> assign('mcode',$mcode);
		}
		// 文章分类
		$cate = Db::name('category')
            ->field('id,name,pid,mcode')
            ->order('sort ASC,id ASC')
            ->select();
        $cate = tree_cate($cate);
        $this->view->assign('cate',$cate);
		$this -> assign('cid', $cid);
		return view('add');
	}
	
	// 保存新增文章
	public function addsave() {
		
		$data = input('post.');
		
// 		var_dump($inputs);die();
		
		$data['time_create'] = $data['time_report'] = $data['time_update'] = time();
		
		// 使用模型验证器进行验证
		$result = $this -> validate($data, 'Article.add');
		if(true !== $result){
			// 验证失败 输出错误信息
			$this -> error($result);
		}
		$cid = $data['cid'];
		if($cid){
		    //查找栏目相关数据
            $list = Db::name('category')
                ->alias('c')
                ->join('model m','c.mcode = m.id')
                ->join('field f','c.mcode = f.moduleid')
                ->field('c.mcode,m.tablename as m_table,f.*')
                ->where('c.id','=',$cid)
                ->order(['f.id'=>'asc'])
                ->select();
		    //循环判断数据合法性
            foreach ($list as $k=>$v){
                //判断是否必填
                if($v['isrequire']==1 ){
                    if(array_key_exists($v['field'],$data)){
                        if(!$data[$v['field']]){
                            $this->error($v['name'].'为必填项!');
                        }
                    }else{
                        $this->error($v['name'].'为必填项!');
                    }

                }
                if(!empty($data[$v['field']])){
                    $minlength = $v['minlength'];
                    $maxlength = $v['maxlength'];
                    switch ($v['type'])
                    {
                        case 'cid'://栏目
                            $maxlength = $maxlength ? min($maxlength, 5) : 5;
                            $length = strlen($data[$v['field']]);
                            //判断长度是否合法
                            if(! ($length>= $minlength && $length<=$maxlength) ){
                                $this->error($v['name'].'长度超限，最多字符：'.$maxlength);
                            }
                            break;
                        case 'title'://标题
                            $maxlength = $maxlength ? min($maxlength, 255) : 255;
                            $length = strlen($data[$v['field']]);
                            //判断长度是否合法
                            if(! ($length>= $minlength && $length<=$maxlength) ){
                                $this->error($v['name'].'长度超限，最多字符：'.$maxlength);
                            }
                            break;
                        case 'text'://单行文本
                            $maxlength = $maxlength ? min($maxlength, 65535) : 65535;
                            $length = strlen($data[$v['field']]);
                            //判断长度是否合法
                            if(! ($length>= $minlength && $length<=$maxlength) ){
                                $this->error($v['name'].'长度超限，最多字符：'.$maxlength);
                            }
                            break;
                        case 'textarea'://多行文本
                            $v['setup'] = string2array($v['setup']);
                            $maxnum = $v['setup']['fieldtype']=='mediumtext' ? 16777215 : 65535;
                            $maxlength = $maxlength ? min($maxlength, $maxnum) : $maxnum;
                            $length = strlen($data[$v['field']]);
                            //判断长度是否合法
                            if(! ($length>= $minlength && $length<=$maxlength) ){
                                $this->error($v['name'].'长度超限，最多字符：'.$maxlength);
                            }
                            break;
                        case 'editor'://编辑器
                            $maxlength = $maxlength ? min($maxlength, 65535) : 65535;
                            $length = strlen($data[$v['field']]);
                            //判断长度是否合法
                            if(! ($length>= $minlength && $length<=$maxlength) ){
                                $this->error($v['name'].'长度超限，最多字符：'.$maxlength);
                            }
                            break;
                        case 'select'://下拉列表
                            $maxlength = $maxlength ? min($maxlength, 255) : 255;
                            $length = strlen($data[$v['field']]);
                            //判断长度是否合法
                            if(! ($length>= $minlength && $length<=$maxlength) ){
                                $this->error($v['name'].'长度超限，最多字符：'.$maxlength);
                            }
                            break;
                        case 'radio'://单选按钮
                            $maxlength = $maxlength ? min($maxlength, 255) : 255;
                            $length = strlen($data[$v['field']]);
                            //判断长度是否合法
                            if(! ($length>= $minlength && $length<=$maxlength) ){
                                $this->error($v['name'].'长度超限，最多字符：'.$maxlength);
                            }
                            break;
                        case 'checkbox'://复选框
                            if(array_key_exists($v['field'],$data)){
                                //dump($data[$v['field']]);exit;
                                $data[$v['field']] = is_array($data[$v['field']]) ? implode(",", $data[$v['field']]) : $data[$v['field']];
                                $maxlength = $maxlength ? min($maxlength, 255) : 255;
                                $length = strlen($data[$v['field']]);
                                //判断长度是否合法
                                if(! ($length>= $minlength && $length<=$maxlength) ){
                                    $this->error($v['name'].'长度超限，最多字符：'.$maxlength);
                                }
                            }

                            break;
                        case 'image'://单张图片
                            $maxlength = $maxlength ? min($maxlength, 80) : 80;
                            $length = strlen($data[$v['field']]);
                            
                            //判断长度是否合法
                            if(! ($length>= $minlength && $length<=$maxlength) ){
                                $this->error($v['name'].'长度超限，最多字符：'.$maxlength);
                            }
                            break;
                        case 'images'://多张图片
                            $maxlength = $maxlength ? min($maxlength, 16777215) : 16777215;
                            if(array_key_exists($v['field'],$data)){
                                for ($i=0; $i<count($data[$v['field']]); $i++) {
                                    $images[$i]['image']=$data[$v['field']][$i];
                                    $images[$i]['title']=$data[$v['field'].'_title'][$i];
                                }
                                $data[$v['field']] = json_encode($images);
                                $length = strlen($data[$v['field']]);
                                //判断长度是否合法
                                if(! ($length>= $minlength && $length<=$maxlength) ){
                                    $this->error($v['name'].'长度超限，最多字符：'.$maxlength);
                                }
                                //去除上传图片和文件的无用字段
                                unset($data[$v['field'].'_title']);
                            }else{
                                $data[$v['field']] = '';
                            }
                            break;
                        case 'file'://文件上传
                            $maxlength = $maxlength ? min($maxlength, 80) : 80;
                            $length = strlen($data[$v['field']]);
                            //判断长度是否合法
                            if(! ($length>= $minlength && $length<=$maxlength) ){
                                $this->error($v['name'].'长度超限，最多字符：'.$maxlength);
                            }
                            break;
                        case 'number'://数字
                            $maxlength = $maxlength ? min($maxlength, 10) : 10;
                            $length = strlen($data[$v['field']]);
                            //判断长度是否合法
                            if(! ($length>= $minlength && $length<=$maxlength) ){
                                $this->error($v['name'].'长度超限，最多字符：'.$maxlength);
                            }
                            break;
                        case 'datetime'://时间
                            $data[$v['field']] = strtotime($data[$v['field']]);
                            $maxlength = $maxlength ? min($maxlength, 11) : 11;
                            $length = strlen($data[$v['field']]);
                            //判断长度是否合法
                            if(! ($length>= $minlength && $length<=$maxlength) ){
                                $this->error($v['name'].'长度超限，最多字符：'.$maxlength);
                            }
                            break;
                        default:
                    }
                }
            }
		}
		$mid = Db::name('category')->where('id', $cid)->value('mcode');
		$table = Db::name('model')->where('mcode', $mid)->value('tablename');
		if($data){
		    $id = Db::name($table)->insertGetId($data);
		    if($id){
		        $this->success('添加成功!');
		    }else{
		        $this->error('添加失败!');
		    }
		}
	}
	
	// 编辑文章页面
	public function edit() {
	    
	    $id = input('param.id/d');
		if(!$id){
			$this -> error('参数错误');
		}
	    
	    $cid = input('cid') ? input('cid') : 0;
		if($cid !== 0){
		    // 输出对应模型的自定义字段
		    $mcode = get_mcode($cid);
		    $fields = Db::name('field')
                ->where('moduleid','=',$mcode)
                ->order('sort ASC,id ASC')
                ->select();
            foreach ($fields as $k=>$v){
                if($fields[$k]['setup']){
                    $fields[$k]['setup'] = string2array($v['setup']);
                    if(array_key_exists('options',$fields[$k]['setup'])){
                        $fields[$k]['setup']['options'] = explode("\n",$fields[$k]['setup']['options']);
                        foreach ($fields[$k]['setup']['options'] as $kk=>$vv){
                            $fields[$k]['setup']['options'][$kk] = trim_array_element(explode("|",$fields[$k]['setup']['options'][$kk]));

                        }
                    }
                }

            }
            // dump($fields);
            $this -> assign('fields',$fields);
            $this -> assign('cid',input('cid/d'));
            $this -> assign('mcode',$mcode);
            
            $lists = self::$_article -> getDetail($id, $cid);
    		if(!$lists){
    			$this -> error('参数错误');
    		}
    		
    		//处理特殊字段
            foreach ($fields as $k=>$v){
                if(array_key_exists($v['field'],$lists)){
                    if($lists[$v['field']]){
                        if($v['type']=='images'){
                            //echo 1;exit;
                            $lists[$v['field']] = json_decode($lists[$v['field']],true);
                        }
                    }
                }
            }
		}
		
		// 文章分类
		$cate = Db::name('category')
            ->field('id,name,pid,mcode')
            ->order('sort ASC,id ASC')
            ->select();
        $cate = tree_cate($cate);
        $this->view->assign('cate',$cate);
		$this -> assign('lists', $lists);
		return view('add');
	}
	
	// 保存编辑文章
	public function editsave() {
	    
	    if(request()->ispost()){
	        //根据catid获取所有字段
            if(input('cid')){
                $data = input();
                //去除上传图片和文件的无用字段
                if(array_key_exists('file',$data)){
                    unset($data['file']);
                }
                $cid = input('cid');
                //查找栏目相关数据
                $list = Db::name('category')
                ->alias('c')
                ->join('model m','c.mcode = m.id')
                ->join('field f','c.mcode = f.moduleid')
                ->field('c.mcode,m.tablename as m_table,f.*')
                ->where('c.id','=',$cid)
                ->order(['f.id'=>'asc'])
                ->select();
                //循环判断数据合法性
                foreach ($list as $k=>$v){
                    //判断是否必填
                    if($v['required']==1 ){
                        if(array_key_exists($v['field'],$data)){
                            if(!$data[$v['field']]){
                                $this->error($v['name'].'为必填项!');
                            }
                        }else{
                            $this->error($v['name'].'为必填项!');
                        }

                    }
                    if(!empty($data[$v['field']])){
                        $minlength = $v['minlength'];
                        $maxlength = $v['maxlength'];
                        switch ($v['type'])
                        {
                            case 'cid'://栏目
                                $maxlength = $maxlength ? min($maxlength, 5) : 5;
                                $length = strlen($data[$v['field']]);
                                //判断长度是否合法
                                if(! ($length>= $minlength && $length<=$maxlength) ){
                                    $this->error($v['name'].'长度超限，最多字符：'.$maxlength);
                                }
                                break;
                            case 'title'://标题
                                $maxlength = $maxlength ? min($maxlength, 255) : 255;
                                $length = strlen($data[$v['field']]);
                                //判断长度是否合法
                                if(! ($length>= $minlength && $length<=$maxlength) ){
                                    $this->error($v['name'].'长度超限，最多字符：'.$maxlength);
                                }
                                break;
                            case 'text'://单行文本
                                $maxlength = $maxlength ? min($maxlength, 65535) : 65535;
                                $length = strlen($data[$v['field']]);
                                //判断长度是否合法
                                if(! ($length>= $minlength && $length<=$maxlength) ){
                                    $this->error($v['name'].'长度超限，最多字符：'.$maxlength);
                                }
                                break;
                            case 'textarea'://多行文本
                                $v['setup'] = string2array($v['setup']);
                                $maxnum = $v['setup']['fieldtype']=='mediumtext' ? 16777215 : 65535;
                                $maxlength = $maxlength ? min($maxlength, $maxnum) : $maxnum;
                                $length = strlen($data[$v['field']]);
                                //判断长度是否合法
                                if(! ($length>= $minlength && $length<=$maxlength) ){
                                    $this->error($v['name'].'长度超限，最多字符：'.$maxlength);
                                }
                                break;
                            case 'editor'://编辑器
                                $maxlength = $maxlength ? min($maxlength, 65535) : 65535;
                                $length = strlen($data[$v['field']]);
                                //判断长度是否合法
                                if(! ($length>= $minlength && $length<=$maxlength) ){
                                    $this->error($v['name'].'长度超限，最多字符：'.$maxlength);
                                }
                                break;
                            case 'select'://下拉列表
                                $maxlength = $maxlength ? min($maxlength, 255) : 255;
                                $length = strlen($data[$v['field']]);
                                //判断长度是否合法
                                if(! ($length>= $minlength && $length<=$maxlength) ){
                                    $this->error($v['name'].'长度超限，最多字符：'.$maxlength);
                                }
                                break;
                            case 'radio'://单选按钮
                                $maxlength = $maxlength ? min($maxlength, 255) : 255;
                                $length = strlen($data[$v['field']]);
                                //判断长度是否合法
                                if(! ($length>= $minlength && $length<=$maxlength) ){
                                    $this->error($v['name'].'长度超限，最多字符：'.$maxlength);
                                }
                                break;
                            case 'checkbox'://复选框
                                if(array_key_exists($v['field'],$data)){
                                    $data[$v['field']] = is_array($data[$v['field']]) ? implode(",", $data[$v['field']]) : $data[$v['field']];
                                    $maxlength = $maxlength ? min($maxlength, 255) : 255;
                                    $length = strlen($data[$v['field']]);
                                    //判断长度是否合法
                                    if(! ($length>= $minlength && $length<=$maxlength) ){
                                        $this->error($v['name'].'长度超限，最多字符：'.$maxlength);
                                    }
                                }

                                break;
                            case 'image'://单张图片
                                $maxlength = $maxlength ? min($maxlength, 80) : 80;
                                $length = strlen($data[$v['field']]);
                                //判断长度是否合法
                                if(! ($length>= $minlength && $length<=$maxlength) ){
                                    $this->error($v['name'].'长度超限，最多字符：'.$maxlength);
                                }
                                break;
                            case 'images'://多张图片
                                $maxlength = $maxlength ? min($maxlength, 16777215) : 16777215;
                                if(array_key_exists($v['field'],$data)){
                                    for ($i=0; $i<count($data[$v['field']]); $i++) {
                                        $images[$i]['image']=$data[$v['field']][$i];
                                        $images[$i]['title']=$data[$v['field'].'_title'][$i];
                                    }
                                    $data[$v['field']] = json_encode($images);
                                    $length = strlen($data[$v['field']]);
                                    //判断长度是否合法
                                    if(! ($length>= $minlength && $length<=$maxlength) ){
                                        $this->error($v['name'].'长度超限，最多字符：'.$maxlength);
                                    }
                                    //去除上传图片和文件的无用字段
                                    unset($data[$v['field'].'_title']);
                                }else{
                                    $data[$v['field']] = '';
                                }
                                break;
                            case 'file'://文件上传
                                $maxlength = $maxlength ? min($maxlength, 80) : 80;
                                $length = strlen($data[$v['field']]);
                                //判断长度是否合法
                                if(! ($length>= $minlength && $length<=$maxlength) ){
                                    $this->error($v['name'].'长度超限，最多字符：'.$maxlength);
                                }
                                break;
                            case 'number'://数字
                                $maxlength = $maxlength ? min($maxlength, 10) : 10;
                                $length = strlen($data[$v['field']]);
                                //判断长度是否合法
                                if(! ($length>= $minlength && $length<=$maxlength) ){
                                    $this->error($v['name'].'长度超限，最多字符：'.$maxlength);
                                }
                                break;
                            case 'datetime'://时间
                                $data[$v['field']] = strtotime($data[$v['field']]);
                                $maxlength = $maxlength ? min($maxlength, 11) : 11;
                                $length = strlen($data[$v['field']]);
                                //判断长度是否合法
                                if(! ($length>= $minlength && $length<=$maxlength) ){
                                    $this->error($v['name'].'长度超限，最多字符：'.$maxlength);
                                }
                                break;
                            default:
                        }
                    }
                }
                $mid = Db::name('category')->where('id', input('cid'))->value('mcode');
		        $table = Db::name('model')->where('mcode', $mid)->value('tablename');
            }
            //入库操作
            if($data){
		        $data['time_update'] = time();
                $result = Db::name($table)
                    ->where('id',$data['id'])
                    ->update($data);
                if($result){
                    $this->success('修改成功!',url('index', ['cid' => $data['cid']]));
                }else{
                    $this->error('修改失败!');
                }
            }
	    }else{
	        $this -> error('参数错误');
	    }
		
	}
	
	// 编辑文章状态
	public function editstatus() {
		
		$id = input('post.id/d');
		if(!$id){
			$this -> error('参数错误');
		}
		
		$result = self::$_article -> editStatus($id);
		if($result){
			$this -> success('操作成功');
		}
		$this -> error('操作失败');
	}
	
	// 删除文章
    public function deletes() {
        
        $id = input('post.id/d');
        $cid = input('post.cid/d');
        if(!$id){
            return ['error'=>1,'msg'=>'参数错误'];
        }
        $mid = Db::name('category')->where('id', $cid)->value('mcode');
        $table = Db::name('model')->where('mcode', $mid)->value('tablename');
        $result = self::$_article -> deletes($id,$table);
        if($result['status'] == 1){
            return ['error'=>0,'msg'=>'删除成功!'];
        }
        return ['error'=>1,'msg'=>$result['msg']];
    }
	
	// 文章分类
	private function categoryListsadd($pickId = 0) {
		$lists = self::$_category -> categoryLists();
		$html .= '<option value="0">--选择分类--</option>';
		foreach($lists as $key => $val){
			// 被选中
			$selected = '';
			if($pickId == $val['id']){
				$selected = 'selected';
			}
			
			// 如果有子分类
			if(self::$_category -> hasChild($val['id'])){
				$html .= '<option value="' . $val['id'] . '" disabled ' . $selected . '>' . get_tree_icons($val['path']) . $val['name'] . '</option>';
			}else{
				$html .= '<option value="' . $val['id'] . '" ' . $selected . '>' . get_tree_icons($val['path']) . $val['name'] . '</option>';
			}
		}
		
		return $html;
	}
	
// 	// 文章分类
// 	private function categoryLists($pickId = 0) {
// 		$lists = self::$_category -> categoryLists();
		
// 		$html = '<select name="cid" id="cid" class="form-control width300">';
// 		$html .= '<option value="0">--选择分类--</option>';
// 		foreach($lists as $key => $val){
// 			// 被选中
// 			$selected = '';
// 			if($pickId == $val['id']){
// 				$selected = 'selected';
// 			}
			
// 			// 如果有子分类
// 			if(self::$_category -> hasChild($val['id'])){
// 				$html .= '<option value="' . $val['id'] . '" disabled ' . $selected . '>' . get_tree_icons($val['path']) . $val['name'] . '</option>';
// 			}else{
// 				$html .= '<option value="' . $val['id'] . '" ' . $selected . '>' . get_tree_icons($val['path']) . $val['name'] . '</option>';
// 			}
// 		}
// 		$html .= '</select>';
		
// 		return $html;
// 	}
	
	// 带参数的上传图片，并保存到数据库中
	public function upimage() {
		
		
		
		// 执行上传
		$result = upload_img();
		
		// 上传成功
		if($result['status'] == 1){
			$result['imgurl'] = $result['url'];
			
			if($id = input('post.aid', 0, 'intval')){   // 编辑页面上传
    		    $lists = self::$_article -> where(array('id' => $id)) -> column('id, litpic');
    		    // 保存数据
    			$saveData = array('litpic' => $result['url'], 'time_update' => time());
    			$saveRes = self::$_article -> save($saveData, array('id' => $id));
    			// 保存成功
    			if($saveRes){
    				// 判断旧图片是否存在，存在则删除
    				if(file_exists('.' . $lists['litpic'])){
    					@unlink('.' . $lists['litpic']);
    				}
    			}
    		}
			
			
		}
		
		// 返回JSON数据格式
		header('Content-Type:application/json; charset=utf-8');
		exit(json_encode($result, 0));
	}
	
	// 图片集 多图上传
	public function uploadimgs()
	{
	    // 执行上传
		$result = upload_imgs();
		
		// 上传成功
		if($result['status'] == 1){
			$result['imgurl'] = $result['url'];
		}
		echo json_encode($result, 0); 
	}
	public function uploadimgs1()
	{
	    $typeArr = array("jpg", "png", "gif");//允许上传文件格式 
        $path = 'uploads/article/';//上传路径 
          
        if (isset($_POST)) { 
            $name = $_FILES['file']['name']; 
            $size = $_FILES['file']['size']; 
            $name_tmp = $_FILES['file']['tmp_name']; 
            if (empty($name)) { 
                echo json_encode(array("error"=>"您还未选择图片")); 
                exit; 
            } 
            $type = strtolower(substr(strrchr($name, '.'), 1)); //获取文件类型 
              
            if (!in_array($type, $typeArr)) { 
                echo json_encode(array("error"=>"清上传jpg,png或gif类型的图片！")); 
                exit; 
            } 
            if ($size > (500 * 1024)) { 
                echo json_encode(array("error"=>"图片大小已超过500KB！")); 
                exit; 
            } 
              
            $pic_name = time() . rand(10000, 99999) . "." . $type;//图片名称 
            $pic_url = $path . $pic_name;//上传后图片路径+名称 
            if (move_uploaded_file($name_tmp, $pic_url)) { //临时文件转移到目标文件夹 
                echo json_encode(array("error"=>"0","pic"=>'/'.$pic_url,"name"=>$pic_name)); 
            } else { 
                echo json_encode(array("error"=>"上传有误，清检查服务器配置！")); 
            } 
        }
	}
	
}
